package com.cbts.service.info.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.cbts.dao.GiftDao;
import com.cbts.dao.RuleDao;

import com.cbts.entity.Gift;
import com.cbts.entity.Rule;
import com.cbts.service.info.ExchangeManagementService;

/**
 * @author 邱鸿辉
 *
 */
public class ExchangeManagementServiceImpl implements ExchangeManagementService{

	private GiftDao giftDao;
	private RuleDao ruleDao;

	public GiftDao getGiftDao() {
		return giftDao;
	}

	public void setGiftDao(GiftDao giftDao) {
		this.giftDao = giftDao;
	}

	public RuleDao getRuleDao() {
		return ruleDao;
	}

	public void setRuleDao(RuleDao ruleDao) {
		this.ruleDao = ruleDao;
	}

	public boolean addGift(Gift gift) {
		
		if(giftDao.save(gift))
			return true;
		return false;
	}

	public boolean deleteGift(Gift gift) {
		
		if(giftDao.delete(gift))
			return true;
		return false;
	}

	public List<Gift> getAllGift() {
			
		return giftDao.findAll().getPageItems();
	}

	public Gift getGift(int id) {
			
		return giftDao.get(id);
	}

	public Rule getRule(int id) {
			
		return ruleDao.get(id);
	}

	public boolean updateGift(Gift gift) {
			
		if(giftDao.update(gift))
			return true;
		return false;
	}

	public boolean updateRule(Rule rule) {
		
		if(ruleDao.update(rule))
			return true;
		return false;
	}
	public boolean addRule(Rule rule) {
		
		if(ruleDao.save(rule))
			return true;
		return false;
	}
	public List<Gift> getSelectedGiftList(List<Gift> giftList,String check,String operate){
		try {
			String[] arraycheckbox = check.split(", ");//因为被用户勾选的物品的giftId被封装成字符串check,
														//这里对check进行分割，得到被勾选的物品的giftId
			ArrayList<Gift> selectedList = new ArrayList<Gift>();//永无存储被勾选的物品

			for (int i = 0; i < arraycheckbox.length; i++)
			{
				Gift gift = getGift(Integer.parseInt(arraycheckbox[i]));
				if ("修改".equals(operate)&&!giftList.isEmpty())
				{
					for (int j = 0; j < giftList.size(); j++)
					{
						if (giftList.get(j).getGiftId() == Integer.parseInt(arraycheckbox[i]) )
						{
							gift.setGiftName(giftList.get(j).getGiftName());//更改物品的相关信息
							gift.setGiftValue(giftList.get(j).getGiftValue());
							gift.setNumber(giftList.get(j).getNumber());
							gift.setDescription(giftList.get(j).getDescription());
							break;
						}
					}
				}
				selectedList.add(gift);		
			}
			return  selectedList;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public boolean deleteGiftList(List<Gift> giftList) {
		
		if(giftDao.deleteList(giftList))
			return true;
		return false;
	}

	public boolean updateGiftList(List<Gift> giftList) {
		
		boolean b = false;
		List<String> giftNames = new ArrayList<String>();
		try {
			for (int i = 0; i < giftList.size(); i++) {
				giftNames.add(giftList.get(i).getGiftName());
				String s = UUID.randomUUID().toString();
				giftList.get(i).setGiftName(s);
				System.out.println("gift.getGiftName():"+giftList.get(i).getGiftName());
				
			}
			b = giftDao.updateList(giftList);
			if(b)
			{
				for (int i = 0; i < giftList.size(); i++) {
					giftList.get(i).setGiftName(giftNames.get(i));
				}
				b = giftDao.updateList(giftList);
			}
			return b;
		} catch (Exception e) {
			return false;
		}
		
	}

	public Gift getGiftByGiftName(String giftName) {
		
		return giftDao.getGiftByGiftName(giftName);
	}
	public boolean sameGiftNameExist(Integer giftId,String giftName){
		String hql = "and o.giftId != ? and o.giftName = ?";
		return giftDao.queryUnique(hql, new Object[]{giftId,giftName})!=null;
	}
}
